Root Zanli
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
home
/
o5t6x7pgljbm
/
public_html
/
admin
/
app
/
Libraries
/
Filename :
FileStorageSystem.php
back
Copy
<?php namespace App\Libraries; use App\Models\File; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Exception; use Illuminate\Http\UploadedFile; class FileStorageSystem { /** * Store a file and save its details to the database. * * @param mixed $file The file to be stored. * @param string $namespace The namespace for the file. * @param string $storageLocation The storage location ('local' or 's3'). * @return string The file handle (UUID). */ public function storeFile($file, $namespace, $storageLocation = 'local') { try { $fileHandle = Str::uuid()->toString(); $originalFileName = $file->getClientOriginalName(); $filePath = $file->storeAs($namespace, $fileHandle . '.' . $file->getClientOriginalExtension(), $storageLocation); $fileModel = File::create([ 'file_handle' => $fileHandle, 'original_file_name' => $originalFileName, 'storage_location' => $storageLocation, 'mime_type' => $file->getMimeType(), 'file_size_bytes' => $file->getSize(), 'namespace' => $namespace, 'file_path' => $filePath, ]); return $fileModel; } catch (Exception $e) { throw new Exception("Failed to store file: " . $e->getMessage()); } } /** * Delete a file from storage and remove its record from the database. * * @param string $fileHandle The file handle (UUID). * @return bool */ public function deleteFile($fileHandle) { $fileRecord = File::where('file_handle', $fileHandle)->first(); if (!$fileRecord) { throw new Exception("File not found."); } $storageLocation = $fileRecord->storage_location; if (Storage::disk($storageLocation)->exists($fileRecord->file_path)) { Storage::disk($storageLocation)->delete($fileRecord->file_path); } return $fileRecord->delete(); } /** * Retrieve a file's content from storage. * * @param string $fileHandle The file handle (UUID). * @return mixed The file content. */ public function retrieveFile($fileHandle) { $fileRecord = File::where('file_handle', $fileHandle)->first(); if (!$fileRecord) { throw new Exception("File not found."); } $storageLocation = $fileRecord->storage_location; if (!Storage::disk($storageLocation)->exists($fileRecord->file_path)) { throw new Exception("File not found in storage."); } return Storage::disk($storageLocation)->get($fileRecord->file_path); } /** * Retrieve a file's path from storage. * * @param string $fileHandle The file handle (UUID). * @return string The file path. */ public function retrieveFilePath($fileHandle) { $fileRecord = File::where('file_handle', $fileHandle)->first(); if (!$fileRecord) { throw new Exception("File not found."); } return $fileRecord->file_path; } public function retrieveAsUploadedFile($fileHandle) : UploadedFile { $fileRecord = File::where('file_handle', $fileHandle)->first(); $file_path = $this->retrieveFilePath($fileHandle); $local_file_path = storage_path( 'app/'. $file_path); $uploadedFile = new UploadedFile( $local_file_path, $fileRecord->original_file_name, null, null, true ); return $uploadedFile; } } ?>